﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using WebSite.Domain;

namespace WebSite.DAL {
    public class MedicoDAO {
        public static void criarUsuario(Medico medico) {
            string strsql = "INSERT INTO tab_Medico(Nome, CRM, Usuario, Email, Senha) VALUES (@Nome, @CRM, @Usuario, @Email, @Senha);";
            SqlConnection objConnection = ConnectionFactory.obterConexao();
            objConnection.Open();
            SqlCommand objCommand = new SqlCommand(strsql, objConnection);
            objCommand.Parameters.Add(new SqlParameter("@Nome", medico.nome));
            objCommand.Parameters.Add(new SqlParameter("@CRM", medico.crm));
            objCommand.Parameters.Add(new SqlParameter("@Usuario", medico.usuario));
            objCommand.Parameters.Add(new SqlParameter("@Email", medico.email));
            objCommand.Parameters.Add(new SqlParameter("@Senha", medico.senha));
            objCommand.ExecuteNonQuery();
            objConnection.Close();
        }

        public static bool buscarUsuarioCriado(Medico medico) {
            string strsql = "SELECT idMedico FROM tab_Medico WHERE Usuario = @Usuario;";
            using (SqlConnection objConnection = ConnectionFactory.obterConexao()) {
                objConnection.Open();
                using (SqlCommand objCommand = new SqlCommand(strsql, objConnection)) {
                    objCommand.Parameters.Add(new SqlParameter("@Usuario", medico.usuario));

                    SqlDataReader reader = objCommand.ExecuteReader();

                    if (reader.HasRows) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        }


        
        public static int verificaLogin(Medico medico) {
            string strsql = "SELECT idMedico,Usuario, Senha FROM tab_Medico WHERE Usuario = @Usuario and Senha = @Senha";
            int idMedico;

            using (SqlConnection objConnection = ConnectionFactory.obterConexao()) {
                objConnection.Open();
                using (SqlCommand objCommand = new SqlCommand(strsql, objConnection)) {
                    objCommand.Parameters.Add(new SqlParameter("@Usuario", medico.usuario));
                    objCommand.Parameters.Add(new SqlParameter("@Senha", medico.senha));

                    SqlDataReader reader = objCommand.ExecuteReader();

                    if (reader.HasRows) {
                        reader.Read();
                        idMedico = Convert.ToInt32(reader["idMedico"]);
                    } else {
                        idMedico = 0;
                    }

                    return idMedico;
                }
            }
        }


    }
}